package Demo;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyThreadPool {
    private BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();

    public void submit(Runnable runnable) throws InterruptedException {
        // 将任务添加到阻塞队列中
        blockingQueue.put(runnable);
    }

    public MyThreadPool(int m){
        // 创建m个线程
        for (int i = 0; i < m; i++) {
            Thread t = new Thread(() ->{
                // 让它一直扫描阻塞队列
                while (true) {
                    try {
                        // 取出任务,并执行
                        Runnable runnable = blockingQueue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }


}
